import warnings
warnings.filterwarnings('ignore')
import matplotlib
matplotlib.use('Qt5Agg')
import numpy as np
import matplotlib.pyplot as plt

X = np.random.randn(100,1)
y = 5 + 4*X +np.random.randn(100,1)

X_b = np.c_[np.ones((100,1)),X]

a = np.linalg.inv(X_b.T.dot(X_b)).dot(X_b.T).dot(y)
print(a)


# 实际应用
x_new = np.array([[0],[2]
])
x_new = np.c_[np.ones((2,1)),x_new]
print(x_new)

y_predict = x_new.dot(a)
print(y_predict)

plt.plot(x_new,y_predict)
plt.plot(X,y,'b.')
plt.axis([0,1,0,15])

plt.show()


# 实际应用
from sklearn.linear_model import LinearRegression
X1 = 2*np.random.rand(100,1)
X2 = 2*np.random.rand(100,1)
X = np.c_[X1,X2]

y = 4 + 3*X1 + 5+X2 +np.random.randn(100,1)

reg = LinearRegression()
reg.fit(X,y)
print(reg.intercept_,reg.coef_)

X_new = np.array([[0,0],
                  [2,1],
                  [2,4]])
y_prediction = reg.predict(X_new)

# 绘图
plt.plot(X_new[:,0],y_prediction,'r-')
plt.plot(X1,y,'b.')
plt.axis([0,2,0,25])
plt.show()